package general.database;

import general.database.info.LinkInfo;
import general.database.info.NodeInfo;
import general.exceptions.*;//QueryNameExistsException;
import general.art.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;



/**
 * 
 * 
 * @author Thomas Maxwell-Mans
 */
public class SQLiteInterface{


	/** Name of the database. */ 
	//private final static String DB_NAME = "test";

	private final static String KEY = "nkey";
	
	/** Name of the Node Table. */ 
	private final static String NODE_TABLE = "Nodes";

	private final static String TALBE_NODE_NAME = "name";
	
	
	/** Name of the Links Table. */ 
	private final static String LINK_TABLE  = "Links";

	
	
	
	private final static String TALBE_LINK_NODE1 = "Node1";
	
	private final static String TALBE_LINK_NODE2 = "Node2";

	
	
	/**
	 * The name of connection.
	 */
	protected Connection mConnect;

	public SQLiteInterface() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

		Class.forName("org.sqlite.JDBC");
		mConnect = DriverManager.getConnection("jdbc:sqlite:test.db");

	}

	/** 
	 * This method is used to get the node at a given key.
	 * 
	 * @param key   The key of the node.
	 * @return      The node at this key.
	 * @throws SQLException 
	 * @throws NodeNotFoundException 
	 */
	public NodeInfo getNodeInfo(final int key) throws SQLException, NodeNotFoundException {

		Statement statement = mConnect.createStatement();      
		ResultSet results = statement.executeQuery("Select * from " +NODE_TABLE + " where "+ KEY +" = " + key);

		NodeInfo nodeInfo = null;


		if (results.next()){

			nodeInfo = new NodeInfo(key, results.getString(TALBE_NODE_NAME), NodeTypes.WORD);
			
			results.close();
			statement.close();
			

			return nodeInfo;


		}
		results.close();
		statement.close();
		

		//If the node does not exist then throw an exception
		throw new  NodeNotFoundException();       
		
	}

	public List<LinkInfo> getLinkInfo(final int nodeKey) throws SQLException{
	
		Statement statement = mConnect.createStatement();
		List<LinkInfo> linkInfo = new LinkedList<LinkInfo>();
		
		ResultSet results1 = statement.executeQuery("Select * from " + LINK_TABLE + " where " + TALBE_LINK_NODE1 + " = " + nodeKey);
		while (results1.next()){

			linkInfo.add( new LinkInfo(results1.getInt("nkey"), nodeKey, results1.getInt(TALBE_LINK_NODE2)));
			
		}		
		results1.close();
		
		
		ResultSet results2 = statement.executeQuery("Select * from " + LINK_TABLE +  "  where "+ TALBE_LINK_NODE2 + " = " + nodeKey);
		while (results2.next()){

			linkInfo.add( new LinkInfo(results2.getInt("nkey"), results2.getInt(TALBE_LINK_NODE1), nodeKey));
			
		}		
		results2.close();
		
		statement.close();
		
		
		
		return linkInfo;
		
	
	}
	
	/** 
	 * This method is used close connection to the database.
	 * @throws SQLException 
	 * 
	 */
	public void close() throws SQLException {

		if (mConnect != null) {
			mConnect.close();
		}     
	}

	/*public static void main(String [] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NodeNotFoundException{

		SQLiteInterface test = new SQLiteInterface();
		//	NodeInfo newNode = test.getNodeInfo(0);
		//System.out.println(newNode.toString());		

	}*/


}
